//
//  EmoticonViewController.swift
//  test
//
//  Created by weixq on 15/8/11.
//  Copyright © 2015年 weixq. All rights reserved.
//

import UIKit

// 标识符
private let HMEmoticonCellIdentifier = "HMEmoticonCellIdentifier"

class EmoticonViewController: UIViewController {

    // 定义一个选择表情的闭包
    // 选择表情的回调
    var selectedEmoticonCallBack: (emoticon: Emoticon) -> ()
    
    // MARK: - 构造函数
    init(selectedEmoticon: (emoticon: Emoticon) -> ()) {
        // 1. 记录闭包
        selectedEmoticonCallBack = selectedEmoticon
        
        // 2. 调用指定的构造函数
        super.init(nibName: nil, bundle: nil)

    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    // MARK: - 监听方法
    func clickItem(item: UIBarButtonItem) {
        print( item.tag)
        
        // 滚动到对应得分组
        let indexPath  = NSIndexPath(forItem: 0, inSection: item.tag) // 滚动到第几组的第几个单元格
    
        collectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.Left, animated: true)
    }
    
    
    // view的大小是屏幕的大小
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = UIColor.yellowColor()
        setupUI()
    
    }
    
    // MARK: - 搭建界面
    func setupUI(){
        view.addSubview(toolbar)
        view.addSubview(collectionView)
        
        // 自动布局 
        let viewDict = ["cv": collectionView, "tb" : toolbar]
        
        toolbar.translatesAutoresizingMaskIntoConstraints = false
        collectionView.translatesAutoresizingMaskIntoConstraints = false
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[cv]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: viewDict))
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[tb]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: viewDict))
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[cv]-0-[tb(44)]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: viewDict))
        
        
        prepareToolBar()
        prepareCollectionView()
    }
    
    // 准备 cv
    private func prepareCollectionView() {
        
        // 设置自己为数据源代理
        collectionView.dataSource = self
        collectionView.delegate = self
        
         collectionView.backgroundColor = UIColor.whiteColor()
        
        // 注册cell
        collectionView.registerClass(EmoticonCell.self, forCellWithReuseIdentifier: HMEmoticonCellIdentifier)
    }
    
    // 准备工具栏
    private func prepareToolBar() {
        toolbar.tintColor = UIColor.darkGrayColor() 
        
        var items = [UIBarButtonItem]()
        var index = 0
        for p in packages {
            items.append(UIBarButtonItem(title: p.groupName, style: UIBarButtonItemStyle.Plain, target: self, action: "clickItem:"))
            
            items.last?.tag = index++
            
            items.append(UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil))
        }
        items.removeLast()
        toolbar.items = items
    }
    
    // MARK: - 懒加载控件
    // 表情包数据
    private lazy var packages = EmoticonPackage.packages
    
    private lazy var toolbar = UIToolbar()
    private lazy var collectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: HMEmoticonLayout())
    
    private class HMEmoticonLayout: UICollectionViewFlowLayout {
    
        private override func prepareLayout() {
        
            // 设置布局
            let w = collectionView!.bounds.width / 7.0
            
            // 由于浮点数计算的问题，如果是0.5在4s 的模拟器上只能显示两排
            let y = (collectionView!.bounds.height - 3 * w) * 0.5
            
            itemSize = CGSize(width: w, height: w)
            minimumInteritemSpacing = 0
            minimumLineSpacing = 0
            
            sectionInset  = UIEdgeInsets(top: y, left: 0, bottom: y, right: 0)
            
            scrollDirection = UICollectionViewScrollDirection.Horizontal
            collectionView?.pagingEnabled = true
            collectionView?.showsHorizontalScrollIndicator  = false
            
        }
    }
    
}

extension EmoticonViewController : UICollectionViewDataSource, UICollectionViewDelegate {
    
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        // 1. 获取到当前选中的表情
        let emoticon = packages[indexPath.section].emoticons![indexPath.item]
        
        // 2. 完成回调
        selectedEmoticonCallBack(emoticon: emoticon)
        
        // 3. 添加最近表情 - 如果用户使用的是最近的表情，就不参加排序
        if indexPath.section > 0 {
            EmoticonPackage.addFavorate(emoticon)
        }
        
        // 4. 刷新数据,第0分组
        // 用户体验，一个界面会闪动，表情变化位置，用户体验不好
        collectionView.reloadSections(NSIndexSet(index: 0))
    }
    
    
    // 返回表情包得数量
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return packages.count
    }
    
    // 返回每个表情包中，表情的数量
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return packages[section].emoticons?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(HMEmoticonCellIdentifier, forIndexPath: indexPath) as! EmoticonCell
        
        cell.emoticon = packages[indexPath.section].emoticons![indexPath.item]
        
        return cell
    }
}

// 表情cell
private class EmoticonCell: UICollectionViewCell {
    // 表情
    var emoticon: Emoticon? {
        didSet{
            // contentsOfFile 如果路径不存在（图像不存在），会返回nil， setimage  -> 清空按钮的图片
            emoticonButton.setImage(UIImage(contentsOfFile: (emoticon?.imagePath)!), forState: UIControlState.Normal)
            
            // 设置emoji
            emoticonButton.setTitle(emoticon?.emoji, forState: UIControlState.Normal)
            
            // 判断是否是删除按钮
            if emoticon!.removeEmoticon {
                emoticonButton.setImage(UIImage(named: "compose_emotion_delete"), forState: UIControlState.Normal)
                emoticonButton.setImage(UIImage(named: "compose_emotion_delete_highlighted"), forState: UIControlState.Highlighted)
            }
            
        }
    }
    
    // 提示：frame 已经有准确的数值，frame的数值来源是
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        emoticonButton.backgroundColor = UIColor.whiteColor()
        // 设置相对 bounds 的缩进后的rect
        emoticonButton.frame = CGRectInset(bounds, 4, 4)
        // 设置按钮字体,可以调整emoji的现实大小
        emoticonButton.titleLabel?.font = UIFont.systemFontOfSize(32)
        
        // 禁用按钮交互，让collectionView的代理接收交互
        emoticonButton.userInteractionEnabled = false
        
        addSubview(emoticonButton)
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 懒加载控件
    private lazy var emoticonButton = UIButton()

}
